package com.card.client.dialog;

import java.util.Date;

import com.card.client.entry.UserInfo;
import com.card.client.entry.Util;
import com.card.client.service.UserInfoManageService;
import com.card.client.service.UserInfoManageServiceAsync;
import com.card.client.view.UserInfoManageView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class EditUserInfoDialog extends DialogBox {

	private static DialogUiBinder uiBinder = GWT.create(DialogUiBinder.class);

	interface DialogUiBinder extends UiBinder<Widget, EditUserInfoDialog> {
	}

	private final UserInfoManageServiceAsync uis = GWT.create(UserInfoManageService.class);
	
	private Long id;
	
	@UiField Button submit;
	
	@UiField Button closeDialog;
	
	@UiField TextBox name;
	
	@UiField TextBox loginName;
	
	@UiField TextBox pass;
	
	@UiField ListBox competence;
	
	@UiField Label message;
	
	private UserInfoManageView userInfoManageView;

	private UserInfo userInfo;
	
	private UserInfo[] userList;

	public EditUserInfoDialog(String title, String descript, UserInfoManageView userInfoManageView, UserInfo userInfo, int flag) {
		setWidget(uiBinder.createAndBindUi(this));
		setTitle(title);
		setText(descript);
		setGlassEnabled(true);
		setAnimationEnabled(true);
		setModal(true);
		message.setStyleName("descript");
		name.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		loginName.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		pass.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		center();
		competence.addItem("", "0");
		competence.addItem(Util.USER_COMPETENCE_HEIGHT_NAME, Integer.toString(Util.USER_COMPETENCE_HEIGHT));
		competence.addItem(Util.USER_COMPETENCE_MIDDLE_NAME, Integer.toString(Util.USER_COMPETENCE_MIDDLE));
		competence.addItem(Util.USER_COMPETENCE_LOW_NAME, Integer.toString(Util.USER_COMPETENCE_LOW));
		this.userInfoManageView = userInfoManageView;
		submit.setText("添加");
		if(flag == Util.DIALOG_CONTROL_EDIT){
			submit.setText("修改");
			this.userInfo = userInfo;
			setContent();
		}
		uis.getAllUserInfo(new AsyncCallback<UserInfo[]>() {
			public void onSuccess(UserInfo[] result) {
				userList = result;
			}
			public void onFailure(Throwable caught) {}
		});
	}

	@UiHandler("submit")
	void onClickSubmit(ClickEvent e) {
		if(!isValid()){
			return;
		}
		UserInfo ui = new UserInfo();
		ui.setId(id);
		ui.setName(name.getText());
		ui.setLoginName(loginName.getText());
		ui.setPass(pass.getText());
		int index = competence.getSelectedIndex();
		String value = competence.getValue(index);
		ui.setCompetence(Integer.parseInt(value));
		if(null != id){
			ui.setLastLoginTime(userInfo.getLastLoginTime());
			ui.setLastLoginAddress(userInfo.getLastLoginAddress());
			uis.updateUserInfo(ui, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					userInfoManageView.onLoad();
				}
				public void onFailure(Throwable caught) {}
				
			});
		} else {
			ui.setLastLoginTime(new Date());
			ui.setLastLoginAddress("没有记录");
			uis.addUserInfo(ui, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					userInfoManageView.onLoad();
				}
				public void onFailure(Throwable caught) {}
			});
		}
	}
	
	@UiHandler("closeDialog")
	void onClickClose(ClickEvent e) {
		hide();
		userInfoManageView.onLoad();
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
		setContent();
	}

	private void setContent(){
		if(userInfo==null){
			return;
		}
		id = userInfo.getId();
		int index = 0;
		for(int i=0;i<competence.getItemCount();i++){
			String value = competence.getValue(i);
			int tc = Integer.parseInt(value);
			if(tc == userInfo.getCompetence()){
				index = i;
				break;
			}
		}
		competence.setSelectedIndex(index);
		name.setText(userInfo.getName());
		loginName.setText(userInfo.getLoginName());
		pass.setText(userInfo.getPass());
	}
	
	private void setMessage(String msg){
		this.message.setText(msg);
	}
	
	private boolean isValid(){
		setMessage("");
		String check = Util.validInput(name.getText());
		if(null != check){
			name.setFocus(true);
			setMessage("用户名" + check);
			return false;
		}
		
		check = Util.validInput(loginName.getText());
		if(null != check){
			loginName.setFocus(true);
			setMessage("登录名" + check);
			return false;
		}
		
		check = Util.validInput(pass.getText());
		if(null != check){
			pass.setFocus(true);
			setMessage("密码" + check);
			return false;
		}
		
		int index = competence.getSelectedIndex();
		String vlaue = competence.getValue(index);
		if("0".equals(vlaue)){
			pass.setFocus(true);
			setMessage("请指定用户权限！");
			return false;
		}
		if(userList.length>0){
			for(int i=0;i<userList.length;i++){
				UserInfo ui = userList[i];
				if(ui.getId().equals(id)){
					return true;
				}
				if(ui.getName().equals(name.getText())){
					name.setFocus(true);
					setMessage("用户名已经存在，请重新输入！");
					return false;
				}
				if(ui.getLoginName().equals(loginName.getText())){
					loginName.setFocus(true);
					setMessage("登录名已经存在，请重新输入！");
					return false;
				}
			}
		}
		return true;
	}
}
